#pragma once
#include<vector>
namespace my_priority {
	//仿函数对象
	template<class T>
	class less
	{
	public:
		bool operator()(const T& x,const T& y)
		{
			return x < y;
		}
	};
	template<class T>
	class greater
	{
	public:
		bool operator()(const T& x, const T& y)
		{
			return x > y;
		}
	};



	//增加一个仿函数参数
	template<class T,class Container = std::vector<T>, class Compare = less<int>>
	class priority_queue {
	public:

		priority_queue()
		{}
		template <class InputIterator>
			priority_queue(InputIterator first, InputIterator last)
				:_con(first,last)
			{
				//建堆  向下建堆
				for (int i = ((_con.size() - 1 - 1) / 2); i >= 0; --i)
				{
					adjust_down(i);
				}
			}
			//向下调整
			void adjust_down(size_t parent)
			{
				Compare com;
			size_t child = parent * 2 + 1;
			while (child < _con.size() )
			{
				//if (child+1 < _con.size() && _con[child]<_con[child+1])
				if (child + 1 < _con.size() && com(_con[child],_con[child + 1]))

				{
					++child;
				}
				//if (_con[parent]<_con[child])
				if(com(_con[parent],_con[child]))
				{
					swap(_con[child], _con[parent]);
					parent = child;
					child = parent * 2 + 1;
				}
				else
				{
					break;
				}
			}

			}
        //向上调整
		void adjust_up(size_t child)
		{
			Compare com;
			int parent = (child - 1) / 2;
			while (child > 0)
			{
				//if (_con[child] > _con[parent])  大堆
				if (com(_con[parent], _con[child]))  //大堆  less--》——_con[parent]<_con[child]
				{
					swap(_con[child], _con[parent]);
					child = parent;
					parent = (child - 1) / 2;
				}
				else {
					break;
				}
			}
		}

		void push(const T& val)
		{
			_con.push_back(val);
			adjust_up(_con.size() - 1);
		}

		const size_t size() const
		{
			return _con.size();
		}

		void pop()
		{
			swap(_con[0], _con[_con.size() - 1]);
			_con.pop_back();
			adjust_down(0);
		}

		const T& top()
		{
			return _con[0];
		}
		bool empty() const
		{
			return _con.empty();
		}
	private:
		Container _con;
	};
}